/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newAdvert.java
 *
 * Created on 2010-06-05, 10:58:50
 */

package adbus;

import adbus.models.FormatList;
import adbus.models.Kampania;
import adbus.models.Pojazd;
import adbus.models.PojazdList;
import adbus.models.Reklama;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *klasa odpowiedzialna z wyswietlanie okna dodania nowej reklamy do nowej kampanii
 * i obsluge zdarzen z nim zwiazanych
 * @author rafal
 */
public class newAdvert extends javax.swing.JDialog {

    /** Creates new form newAdvert */
    public newAdvert(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /**dwuargumentowy konstruktor newAdvert*/
    public newAdvert(java.awt.Frame parent, Kampania c) {
        super(parent, true);
        campaign = c;
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        carrierLabel = new javax.swing.JLabel();
        carriers = new javax.swing.JComboBox();
        lineLabel = new javax.swing.JLabel();
        lines = new javax.swing.JComboBox();
        vehicleLabel = new javax.swing.JLabel();
        vehicle = new javax.swing.JComboBox();
        formatsLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        formats = new javax.swing.JList();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 360));
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(adbus.AdBusApp.class).getContext().getResourceMap(newAdvert.class);
        carrierLabel.setText(resourceMap.getString("carrierLabel.text")); // NOI18N
        carrierLabel.setName("carrierLabel"); // NOI18N

        carriers.setModel(campaign.pobierzDostepnychPrzewoznikow());
        carriers.setName("carriers"); // NOI18N
        carriers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carriersActionPerformed(evt);
            }
        });

        lineLabel.setText(resourceMap.getString("lineLabel.text")); // NOI18N
        lineLabel.setName("lineLabel"); // NOI18N

        lines.setModel(campaign.pobierzDostepneLinie());
        lines.setName("lines"); // NOI18N
        lines.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linesActionPerformed(evt);
            }
        });

        vehicleLabel.setText(resourceMap.getString("vehicleLabel.text")); // NOI18N
        vehicleLabel.setName("vehicleLabel"); // NOI18N

        vehicle.setModel(campaign.pobierzDostepnePojazdy());
        vehicle.setName("vehicle"); // NOI18N
        vehicle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vehicleActionPerformed(evt);
            }
        });

        formatsLabel.setText(resourceMap.getString("formatsLabel.text")); // NOI18N
        formatsLabel.setName("formatsLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        formats.setModel((((Pojazd)getPojazdList().getSelectedItem())==null)?new DefaultListModel():((Pojazd)getPojazdList().getSelectedItem()).pobierzFormaty());
        formats.setName("formats"); // NOI18N
        jScrollPane1.setViewportView(formats);

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(carrierLabel)
                    .addComponent(lineLabel)
                    .addComponent(vehicleLabel)
                    .addComponent(formatsLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(vehicle, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lines, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(carriers, 0, 225, Short.MAX_VALUE)))
                .addContainerGap(90, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(218, Short.MAX_VALUE)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(carriers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(carrierLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lineLabel)
                    .addComponent(lines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vehicleLabel)
                    .addComponent(vehicle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(formatsLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**metoda odpowiedzialna za obsluge listy rozwijalnej przewoznikow*/
    private void carriersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carriersActionPerformed
        campaign.pobierzDostepneLinie();
        campaign.pobierzDostepnePojazdy();
        if(((Pojazd)vehicle.getSelectedItem())!=null)
            ((Pojazd)vehicle.getSelectedItem()).pobierzFormaty();
    }//GEN-LAST:event_carriersActionPerformed
/**metoda odpowiedzialna za obsluge przycisku anuluj*/
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed
/**metoda odpowiedzialna za obsluge przycisku ok*/
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if(formats.isSelectionEmpty()) {
            JOptionPane.showMessageDialog(this, "Wybierz co najmniej jeden format", "Ostrzeżenie", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        Reklama adv = new Reklama();
        adv.przypiszPojazd((Pojazd) vehicle.getSelectedItem());
        adv.przypiszFormaty(formats.getSelectedValues());
        campaign.przypiszReklame(adv);
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed
/**metoda odpowiedzialna za obsluge listy rozwijalnej linii*/
    private void linesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_linesActionPerformed
        // TODO add your handling code here:
        campaign.pobierzDostepnePojazdy();
        //((Pojazd)vehicle.getSelectedItem()).pobierzFormaty();
        formats.setModel(((Pojazd)vehicle.getSelectedItem()).pobierzFormaty());
        ((FormatList)formats.getModel()).reload();
    }//GEN-LAST:event_linesActionPerformed
/**metoda odpowiedzialna za obsluge listy rozwijalnej pojazdow*/
    private void vehicleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vehicleActionPerformed
        // TODO add your handling code here:
        ((Pojazd)vehicle.getSelectedItem()).pobierzFormaty();
    }//GEN-LAST:event_vehicleActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                newAdvert dialog = new newAdvert(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel carrierLabel;
    private javax.swing.JComboBox carriers;
    private javax.swing.JList formats;
    private javax.swing.JLabel formatsLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lineLabel;
    private javax.swing.JComboBox lines;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox vehicle;
    private javax.swing.JLabel vehicleLabel;
    // End of variables declaration//GEN-END:variables
    private Kampania campaign = null;
/**metoda odpowiedzialna za odswiezanie*/
    void reload() {
        //((PrzewoznikList) carriers.getModel()).setList(campaign.pobierzDostepnychPrzewoznikow().getList());
        campaign.pobierzDostepnychPrzewoznikow();
        campaign.pobierzDostepneLinie();
    }
/**metoda odpowiedzialna za pobieranie modelow pojazdow*/
    PojazdList getPojazdModel() {
        return (PojazdList) vehicle.getModel();
    }
/**metoda odpowiedzialna za pobieranie listy pojadzdow*/
    JComboBox getPojazdList() {
        return vehicle;
    }
}
